package com.weixiao.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.weixiao.domain.Goods;
import com.weixiao.service.GoodsService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class SecKillListener {
    @Autowired
    private GoodsService goodsService;

    //接收消息，并调用方法执行业务
    @RabbitListener(queues = "Q")
    public void receive(Message message) {
        //从msg得到秒杀请求中传来的用户id跟商品id，进行实际的购买业务处理
        String msg = new String(message.getBody());
        JSONObject jsonObject = JSONObject.parseObject(msg);
        Integer userId = (Integer)jsonObject.get("userId");
        Integer goodsId = (Integer)jsonObject.get("goodsId");
        //调用方法执行业务
        System.out.println(userId + " " + goodsId);
        goodsService.realDoseckill(userId, goodsId);
    }
}
